<template>
  <list
    v-model:loading="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
    class="list"
  >
    <GoodsItem v-for="goods in goodsList" :key="goods.id" :goods="goods" />
  </list>
</template>

<script>
import { ref } from 'vue'
import { List } from 'vant'
import { getHomeData } from '@/api/home'
import GoodsItem from '@/components/library/GoodsItem.vue'
export default {
  name: 'GoodsList',
  components: { List, GoodsItem },
  props: {
    // 分类，相当于作用为请求参数
    type: {
      type: Object,
      default: () => ({ sales: 1 })
    }
  },
  setup (props) {
    const goodsList = ref([])
    const loading = ref(false)
    const finished = ref(false)
    let page = 1
    const onLoad = () => {
      // 加载中
      getHomeData({ ...props.type, page }).then((res) => {
        if (res.data.goods.data.length) {
          goodsList.value.push(...res.data.goods.data)
          page++
        } else {
          finished.value = true
        }
        loading.value = false
      })
    }
    return {
      loading,
      finished,
      onLoad,
      goodsList
    }
  }
}
</script>

<style scoped lang='less'>
.list {
  margin-bottom: 60px;
}
</style>
